/****************************************************************************
 * SaveGame Manager GX 2012-2013
 * by dj_skual
 *
 * ManageProgressWindow.cpp
 ***************************************************************************/
#include "ManageProgressWindow.h"
#include "../Controls/Application.h"

ManageProgressWindow::ManageProgressWindow(std::string Title, std::string Message, bool cancelable)
	: GuiFrame(572, 372)
	, title(Title)
	, message(Message)
	, Visible(false)
{
	cancel = NULL;
	SetCancel(cancelable);
	
	throbber = new ProgressThrobber();
	throbber->SetAlignment(ALIGN_RIGHT | ALIGN_BOTTOM);
	throbber->SetPosition(-30, -30);
	Append(throbber);
	
	SetAlignment(ALIGN_CENTER | ALIGN_MIDDLE);
	SetPosition(6, 0);
	
	Show();
}

ManageProgressWindow::~ManageProgressWindow()
{
	if(Visible)
		Hide();
	
	RemoveAll();
	
	delete cancel;
	delete throbber;
}

void ManageProgressWindow::Show()
{
	if(Visible)
		return;
	Visible = true;
	
	SetEffect(EFFECT_FADE, 20);
	Application::Instance()->Append(this);
	Application::Instance()->SetUpdateOnly(this);
}

void ManageProgressWindow::Hide()
{
	if(!Visible)
		return;
	Visible = false;
	
	SetEffect(EFFECT_FADE, -20);
	while(cancel->IsAnimated())
		Application::Instance()->updateEvents();
	
	Application::Instance()->Remove(this);
	Application::Instance()->UnsetUpdateOnly(this);
}

void ManageProgressWindow::SetCancel(bool cancelable)
{
	if(cancel)
	{
		Remove(cancel);
		delete cancel;
	}
	
	cancel = new ManageButtons(title.c_str() , message.c_str(), cancelable ? tr( "Cancel" ) : 0);
	cancel->SetAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
	cancel->SetPosition(-6, -30);
	cancel->ButtonClicked.connect(this, &ManageProgressWindow::OnClick);
	Append(cancel);
}

void ManageProgressWindow::SetTitle(std::string Title)
{
	title = Title;
	if(cancel)
		cancel->SetTitle(title);
}

void ManageProgressWindow::SetMessage(std::string Msg)
{
	message = Msg;
	if(cancel)
		cancel->SetMessage(message);
}

void ManageProgressWindow::OnClick(ManageButtons * buttons UNUSED, int choice UNUSED)
{
	Canceled();
}
